package com.example.onlinemarket.service;

import com.example.onlinemarket.bean.mobile.EasyOrderItemInfo;
import com.example.onlinemarket.dao.GoodDao;
import com.example.onlinemarket.dao.OrderItemDao;
import com.example.onlinemarket.bean.OrderItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderItemService {
    private OrderItemDao orderItemDao;
    private GoodDao goodDao;
    @Autowired
    public void setOrderItemDao(OrderItemDao orderItemDao){
        this.orderItemDao = orderItemDao;
    }
    @Autowired
    public void setGoodDao(GoodDao goodDao) { this.goodDao = goodDao; }

    public List<OrderItem> getOrderItemsByOrderId(Integer orderId){
        return orderItemDao.getOrderItemsByOrderId(orderId);
    }

    public String addOrderItem(Integer orderId, Integer goodId, Integer goodNum) {
        boolean flag = orderItemDao.checkOrderItemExist(orderId, goodId) > 0;//查询该订单下有无该鲜花
        String msg = null;
        //若没有该鲜花则添加鲜花，否则更新鲜花数量
        if(!flag){
            if(goodNum>0) {//不进行非法操作，避免产生非法数据
                if(goodDao.isGoodUpshelf(goodId)) {
                    flag = orderItemDao.addOrderItem(orderId, goodId, goodNum);
                }else{
                    msg = "鲜花已下架，无法购买！";
                    flag = false;
                }
            }else{
                msg = "鲜花数量不合法！";
                flag = false;
            }
        }else{
            int orderItemNum = orderItemDao.getOrderItemNum(orderId, goodId);
            if(goodNum>0||(goodNum<0&&orderItemNum>((-1)*goodNum))) {
                //允许提交鲜花数量为负数，但其绝对值必须小于原鲜花数量
                flag = orderItemDao.updateOrderItemNum(orderId, goodId, goodNum);
            }else{
                msg = "鲜花数量不合法！";
                flag = false;
            }
        }
        return msg;
    }

    public boolean cleanShoppingCart(Integer orderId) {
        return orderItemDao.delOrderItemsByOrderId(orderId);
    }

    public boolean updateGoodNum(Integer orderId, Integer goodId, Integer goodNum) {
        return orderItemDao.updateGoodNum(orderId, goodId, goodNum);
    }

    public boolean updateGoodNote(Integer orderId, Integer goodId, String note) {
        return orderItemDao.updateNote(orderId, goodId, note);
    }

    public boolean updateShopGoodStatus(Integer userId, Integer orderId, String goodStatus) {
        return orderItemDao.updateShopGoodStatus(userId, orderId, goodStatus);
    }

    public boolean updateOrderItemStatus(Integer orderId, String goodStatus) {
        return orderItemDao.updateOrderItemStatus(orderId, goodStatus);
    }

    public boolean delOrderItemsByOrderId(Integer orderId) {
        return orderItemDao.delOrderItemsByOrderId(orderId);
    }

    public boolean delOrderItem(Integer orderId, Integer goodId){ return orderItemDao.delOrderItem(orderId, goodId); }

    //更新购物车状态
    public void updateOrderItemInfos(int orderId, List<EasyOrderItemInfo> orderItemInfoList) {
        for(EasyOrderItemInfo orderItemInfo: orderItemInfoList){
            if(orderItemInfo.isDel())
                orderItemDao.delOrderItem(orderId, orderItemInfo.getGoodId());
            else {
                boolean ok = orderItemDao.updateOrderItemInfo(orderId, orderItemInfo);
                if(!ok)
                    orderItemDao.addOrderItem(orderId, orderItemInfo.getGoodId(), orderItemInfo.getGoodNum());
            }
        }
    }
}
